package pl.us.ij.plugins.segmentation;

import ij.IJ;

import java.io.Serializable;

public class Trace implements Serializable {
    public static boolean _logging = false;

    public static void log(String s) {
        if (_logging) {
            IJ.log(s);
        }
    }

    public static void logPixels(String title, float[] pixels, int cols, int rows, int rowLen ) {
        String line;

        for ( int y = 0; y < rows; y++ ) {
            line = title;
            for ( int x = 0; x < cols - 1; x++ ) {
                line += pixels[rowLen * y + x] + ", ";
            }
            line += pixels[rowLen * y + cols - 1];

            log(line);
        }
    }

    public static void logPixels(String title, float[][] pixels, int cols, int rows ) {
        float[] onePixels = new float[pixels.length * pixels[0].length];

        int indexer = 0;
        for ( int y = 0; y < pixels[0].length; y ++) {
            for ( int x = 0; x < pixels.length; x ++ ) {
                onePixels[ indexer ++ ] = pixels[y][x];
            }
        }

        cols = cols > pixels[0].length ? pixels[0].length : cols;
        rows = rows > pixels.length ? pixels.length : rows;

        logPixels( title, onePixels, cols, rows, pixels[0].length);
    }

    public static void logPixels(String title, byte[] pixels, int cols, int rows, int rowLen ) {
        String line;

        for ( int y = 0; y < rows; y++ ) {
            line = title;
            for ( int x = 0; x < cols - 1; x++ ) {
                line += pixels[rowLen * y + x] + ", ";
            }
            line += pixels[rowLen * y + cols - 1];

            log(line);
        }
    }

    public static void logPixels(String title, byte[][] pixels, int cols, int rows ) {
        float[] onePixels = new float[pixels.length * pixels[0].length];

        int indexer = 0;
        for ( int y = 0; y < pixels[0].length; y ++) {
            for ( int x = 0; x < pixels.length; x ++ ) {
                onePixels[ indexer ++ ] = pixels[y][x];
            }
        }

        cols = cols > pixels[0].length ? pixels[0].length : cols;
        rows = rows > pixels.length ? pixels.length : rows;

        logPixels( title, onePixels, cols, rows, pixels[0].length);
    }

    public static boolean getLogging() {
        return _logging;
    }

    public static void setLogging(boolean b) {
        _logging = b;
    }
}